#
# r = ShowapiRequest("http://route.showapi.com/852-2","my_appId","my_appSecret" )
# r.addBodyPara("type", "4001")
# r.addBodyPara("page", "1")
# res = r.post()
# print(res.text) # 返回信息

import requests  # 导入requests包
from bs4 import BeautifulSoup
from django.db.models import Count
from django.shortcuts import render
from pandas._libs import json
from scipy.special import comb

from ssqapp.models import Ssq

#双色球
def index(request):
    url = 'http://datachart.500.com/ssq/history/newinc/history.php?start=03001&end=19139'
    strhtml = requests.get(url)
    # print(strhtml.text)
    # soup = BeautifulSoup(strhtml.text, 'lxml')
    soup = BeautifulSoup(strhtml.text, 'lxml')
    tbody = soup.find(id='tdata')
    # print(type(tbody))
    # print(tbody)

    trs = tbody.find_all('tr')
    # print(len(trs))

    # 解析table 取出值
    ssqList = []
    for tr in trs:
        tdlist = []

        tds = tr.find_all('td')
        for td in tds:
            tdlist.append(td.string)
            # print(),

        ssqList.append(tdlist)

    print(type(ssqList))

    for i in range(len(ssqList)):
        temlist = ssqList[i]
        # print(temlist)
        ssq = Ssq()
        ssq.ctype = "双色球"
        ssq.code = temlist[0]
        ssq.red1 = temlist[1]
        ssq.red2 = temlist[2]
        ssq.red3 = temlist[3]
        ssq.red4 = temlist[4]
        ssq.red5 = temlist[5]
        ssq.red6 = temlist[6]
        ssq.blue = temlist[7]
        ssq.starDate = temlist[15]
        # 获所有满足条件的所有对象,判断数据是否存在
        date = Ssq.objects.filter(code=temlist[0], ctype="双色球").exists()

        if date:
            print(date)
        else:
            ssq.save()


    # return render(request, 'chartstest/public.html', {"dataList": data_list})
    return render(request, 'ssq/index.html')


#双色球
def ssqShow(request):
    data_list = []
    result = Ssq.objects.filter(ctype="双色球")
    # print(type(result))
    # print(result)
    for obj in result:
        # print(obj)
        data_list.append(obj)


    r = [0 for x in range(0, 34)]
    b = [0 for x in range(0, 17)]
    print('s-----------------------')
    for i in range(0, len(data_list)):
        # data_list.append(book_list[i][9])
        red1 = data_list[i].red1
        red2 = data_list[i].red2
        red3 = data_list[i].red3
        red4 = data_list[i].red4
        red5 = data_list[i].red5
        red6 = data_list[i].red6
        blue = data_list[i].blue

        for j in range(1, len(r)):
            if red1 == j:
                r[j] = r[j]+1
            if red2 == j:
                r[j] = r[j]+1
            if red3 == j:
                r[j] = r[j]+1
            if red4 == j:
                r[j] = r[j]+1
            if red5 == j:
                r[j] = r[j]+1
            if red6 == j:
                r[j] = r[j]+1


        for k in range(1, len(b)):
            if blue == k:
                b[k] = b[k] + 1






    print(r)
    print(b)

    #print("json:" + json.dumps(data_list))
    #print(data_list)

    #print(comb(33, 6))
    data = {'status': 0, 'rlist': r,"blist":b}

    return render(request, 'ssq/details.html', {"data": json.dumps(data, ensure_ascii=False)})
#统计每个数据出现的概率



#大乐透
def dltShow(request):
    data_list = []
    result = Ssq.objects.filter(ctype="大乐透")
    # print(type(result))
    # print(result)
    for obj in result:
        # print(obj)
        data_list.append(obj)


    r = [0 for x in range(0, 36)]
    b = [0 for x in range(0, 13)]
    c = [0 for x in range(0, 13)]
    print('s-----------------------')
    for i in range(0, len(data_list)):
        # data_list.append(book_list[i][9])
        red1 = data_list[i].red1
        red2 = data_list[i].red2
        red3 = data_list[i].red3
        red4 = data_list[i].red4
        red5 = data_list[i].red5
        red6 = data_list[i].red6
        blue = data_list[i].blue

        for j in range(1, len(r)):
            if red1 == j:
                r[j] = r[j]+1
            if red2 == j:
                r[j] = r[j]+1
            if red3 == j:
                r[j] = r[j]+1
            if red4 == j:
                r[j] = r[j]+1
            if red5 == j:
                r[j] = r[j]+1



        for k in range(1, len(b)):
            if red6 == k:
                b[k] = b[k] + 1
            if blue == k:
                b[k] = b[k] + 1






    print(r)
    print(b)

    #print("json:" + json.dumps(data_list))
    #print(data_list)

    #print(comb(33, 6))
    data = {'status': 0, 'rlist': r,"blist":b}

    return render(request, 'ssq/dltShow.html', {"data": json.dumps(data, ensure_ascii=False)})





#七星彩
def qxc(request):
    #url = 'http://datachart.500.com/ssq/history/newinc/history.php?start=03001&end=19139'
    url = 'http://datachart.500.com/qxc/history/inc/history.php?limit=15040&start = 04101 & end = 19139'
    strhtml = requests.get(url)
    #print(strhtml.text)
    # soup = BeautifulSoup(strhtml.text, 'lxml')
    soup = BeautifulSoup(strhtml.text, 'lxml')
    tbody = soup.find(id='tablelist')
    #print(type(tbody))
    #print(tbody)

    trs = tbody.find_all('tr')
    print(len(trs))

    # 解析table 取出值
    ssqList = []
    for tr in trs:
        tdlist = []

        tds = tr.find_all('td')

        [tds.extract() for tds in soup("span")]
        #print(tds)
        for td in tds:

            tdlist.append(td.string)
            #print(td.string)

        ssqList.append(tdlist)

    #print(type(ssqList))
    #print(ssqList)
    for i in range(1, len(ssqList)):
        temlist = ssqList[i]
        #print(temlist)
        num =[]
        ssq = Ssq()
        ssq.ctype = "七星彩"
        ssq.code = temlist[0]

        num = temlist[1]

        for j in range(len(num)):
            if j == 0:
                ssq.red1 = int(num[j])
            if j == 2:
                ssq.red2 = int(num[j])
            if j == 4:
                ssq.red3 = int(num[j])
            if j == 6:
                ssq.red4 = int(num[j])
            if j == 8:
                ssq.red5 = int(num[j])
            if j == 10:
                ssq.red6 = int(num[j])
            if j == 12:
                ssq.blue = int(num[j])


        ssq.starDate = temlist[4]
        # 获所有满足条件的所有对象,判断数据是否存在
        date = Ssq.objects.filter(code=temlist[0], ctype="七星彩").exists()
        print(ssq)

        if date:
            print(date)
        else:

            ssq.save()

#七星彩
def qxcShow(request):
    data_list = []
    result = Ssq.objects.filter(ctype="七星彩")
    # print(type(result))
    # print(result)
    for obj in result:
        # print(obj)
        data_list.append(obj)


    r = [0 for x in range(0, 10)]
    print('s-----------------------')
    for i in range(0, len(data_list)):
        # data_list.append(book_list[i][9])
        red1 = data_list[i].red1
        red2 = data_list[i].red2
        red3 = data_list[i].red3
        red4 = data_list[i].red4
        red5 = data_list[i].red5
        red6 = data_list[i].red6
        blue = data_list[i].blue

        for j in range(0, len(r)):
            if red1 == j:
                r[j] = r[j]+1
            if red2 == j:
                r[j] = r[j]+1
            if red3 == j:
                r[j] = r[j]+1
            if red4 == j:
                r[j] = r[j]+1
            if red5 == j:
                r[j] = r[j]+1
            if red6 == j:
                r[j] = r[j] + 1
            if blue == j:
                r[j] = r[j] + 1


    print(r)

    #print("json:" + json.dumps(data_list))
    #print(data_list)

    #print(comb(33, 6))
    data = {'status': 0, 'rlist': r}

    return render(request, 'ssq/dltShow.html', {"data": json.dumps(data, ensure_ascii=False)})

#大乐透
def dlt(request):
    url = 'https://datachart.500.com/dlt/history/newinc/history.php?start=04001&end=19140'
    strhtml = requests.get(url)
    # print(strhtml.text)
    # soup = BeautifulSoup(strhtml.text, 'lxml')
    soup = BeautifulSoup(strhtml.text, 'lxml')
    tbody = soup.find(id='tdata')
    # print(type(tbody))
    # print(tbody)

    trs = tbody.find_all('tr')
    # print(len(trs))

    # 解析table 取出值
    ssqList = []
    for tr in trs:
        tdlist = []

        tds = tr.find_all('td')
        for td in tds:
            tdlist.append(td.string)
            # print(),

        ssqList.append(tdlist)

    print(type(ssqList))

    for i in range(len(ssqList)):
        temlist = ssqList[i]
        # print(temlist)
        ssq = Ssq()
        ssq.ctype = "大乐透"
        ssq.code = temlist[0]
        ssq.red1 = temlist[1]
        ssq.red2 = temlist[2]
        ssq.red3 = temlist[3]
        ssq.red4 = temlist[4]
        ssq.red5 = temlist[5]
        ssq.red6 = temlist[6]
        ssq.blue = temlist[7]
        ssq.starDate = temlist[14]
        # 获所有满足条件的所有对象,判断数据是否存在
        date = Ssq.objects.filter(code=temlist[0], ctype="大乐透").exists()

        if date:
            print(date)
        else:
            ssq.save()


#排列5
def pl5(request):
    #url = 'http://datachart.500.com/ssq/history/newinc/history.php?start=03001&end=19139'
    url = 'https://datachart.500.com/plw/history/inc/history.php?limit=16327&start=03001&end=19328'
    strhtml = requests.get(url)
    #print(strhtml.text)
    # soup = BeautifulSoup(strhtml.text, 'lxml')
    soup = BeautifulSoup(strhtml.text, 'lxml')
    tbody = soup.find(id='tablelist')
    #print(type(tbody))
    #print(tbody)

    trs = tbody.find_all('tr')
    print(len(trs))

    # 解析table 取出值
    ssqList = []
    for tr in trs:
        tdlist = []

        tds = tr.find_all('td')

        [tds.extract() for tds in soup("span")]
        #print(tds)
        for td in tds:

            tdlist.append(td.string)
            #print(td.string)

        ssqList.append(tdlist)

    #print(type(ssqList))
    #print(ssqList)
    for i in range(1, len(ssqList)):
        temlist = ssqList[i]
        #print(temlist)
        num =[]
        ssq = Ssq()
        ssq.ctype = "排列5"
        ssq.code = temlist[0]

        num = temlist[1]

        for j in range(len(num)):
            if j == 0:
                ssq.red1 = int(num[j])
            if j == 2:
                ssq.red2 = int(num[j])
            if j == 4:
                ssq.red3 = int(num[j])
            if j == 6:
                ssq.red4 = int(num[j])
            if j == 8:
                ssq.red5 = int(num[j])

        ssq.red6 = 0
        ssq.blue = 0
        ssq.starDate = temlist[4]
        # 获所有满足条件的所有对象,判断数据是否存在
        date = Ssq.objects.filter(code=temlist[0], ctype="排列5").exists()
        print(ssq)

        if date:
            print(date)
        else:

            ssq.save()

#排列5
def pl5Show(request):
    data_list = []
    result = Ssq.objects.filter(ctype="排列5")
    # print(type(result))
    # print(result)
    for obj in result:
        # print(obj)
        data_list.append(obj)


    r = [0 for x in range(0, 10)]
    print('s-----------------------')
    for i in range(0, len(data_list)):
        # data_list.append(book_list[i][9])
        red1 = data_list[i].red1
        red2 = data_list[i].red2
        red3 = data_list[i].red3
        red4 = data_list[i].red4
        red5 = data_list[i].red5
        red6 = data_list[i].red6
        blue = data_list[i].blue

        for j in range(0, len(r)):
            if red1 == j:
                r[j] = r[j]+1
            if red2 == j:
                r[j] = r[j]+1
            if red3 == j:
                r[j] = r[j]+1
            if red4 == j:
                r[j] = r[j]+1
            if red5 == j:
                r[j] = r[j]+1



    print(r)

    #print("json:" + json.dumps(data_list))
    #print(data_list)

    #print(comb(33, 6))
    data = {'status': 0, 'rlist': r}

    return render(request, 'ssq/dltShow.html', {"data": json.dumps(data, ensure_ascii=False)})

#3d
def sd(request):
    #url = 'http://datachart.500.com/ssq/history/newinc/history.php?start=03001&end=19139'
    url = 'https://datachart.500.com/sd/history/inc/history.php?limit=15327&start=2004001&end=2019328'
    #url = 'https://datachart.500.com/sd/history/inc/history.php?limit=27&start=2019301&end=2019328'
    strhtml = requests.get(url)
    #print(strhtml.text)
    # soup = BeautifulSoup(strhtml.text, 'lxml')
    soup = BeautifulSoup(strhtml.text, 'lxml')
    tbody = soup.find(id='tablelist')
    #print(type(tbody))
    #print(tbody)

    trs = tbody.find_all('tr')
    print(len(trs))
    print(trs)

    # 解析table 取出值
    ssqList = []
    for tr in trs:
        tdlist = []

        tds = tr.find_all('td')

        [tds.extract() for tds in soup("span")]
        #print(tds)
        for td in tds:

            tdlist.append(td.string)
            #print(td.string)

        ssqList.append(tdlist)

    #print(type(ssqList))
    #print(ssqList)
    for i in range(2, len(ssqList)):
        temlist = ssqList[i]
        #print(temlist)
        num =[]
        ssq = Ssq()
        ssq.ctype = "福彩3D"
        ssq.code = temlist[0]

        num = temlist[1]
        print(num)
        for j in range(len(num)):

            if j == 0:
                ssq.red1 = int(num[j])
            if j == 2:
                ssq.red2 = int(num[j])
            if j == 4:
                ssq.red3 = int(num[j])

        ssq.red4 = 0
        ssq.red5 = 0
        ssq.red6 = 0
        ssq.blue = 0
        ssq.starDate = temlist[10]
        # 获所有满足条件的所有对象,判断数据是否存在
        date = Ssq.objects.filter(code=temlist[0], ctype="福彩3D").exists()
        print(ssq)

        if date:
            print(date)
        else:

            ssq.save()


# sd
def sdShow(request):
    data_list = []
    result = Ssq.objects.filter(ctype="福彩3D")
    # print(type(result))
    # print(result)
    for obj in result:
        # print(obj)
        data_list.append(obj)

    r = [0 for x in range(0, 10)]
    print('s-----------------------')
    for i in range(0, len(data_list)):
        # data_list.append(book_list[i][9])
        red1 = data_list[i].red1
        red2 = data_list[i].red2
        red3 = data_list[i].red3
        red4 = data_list[i].red4
        red5 = data_list[i].red5
        red6 = data_list[i].red6
        blue = data_list[i].blue

        for j in range(0, len(r)):
            if red1 == j:
                r[j] = r[j] + 1
            if red2 == j:
                r[j] = r[j] + 1
            if red3 == j:
                r[j] = r[j] + 1


    print(r)

    # print("json:" + json.dumps(data_list))
    # print(data_list)

    # print(comb(33, 6))
    data = {'status': 0, 'rlist': r}

    return render(request, 'ssq/sdShow.html', {"data": json.dumps(data, ensure_ascii=False)})